<template>
  <div class="bottom-wrapper" :class="{'hide-play': !showPlay}">
    <div class="bottom-play-wrapper"
         @click.stop.prevent="onPlayingCardClick"
         v-if="showPlay">
      <div class="bottom-playing-left">
        <div class="icon-play-wrapper"
             @click.stop.prevent="togglePlay">
          <span class="iconfont icon-play_go" v-if="!isPlaying">&#xe66a;</span>
          <span class="iconfont icon-play_pause" v-else>&#xe78e;</span>
        </div>
      </div>
      <div class="bottom-playing-right">
        <div class="bottom-playing-chapter-text">
          <span class="chapter-label">{{chapter ? chapter.label : ''}}</span>
          <span class="bottom-playing-page-text" v-if="currentSectionIndex && currentSectionTotal">( {{currentSectionIndex}} / {{currentSectionTotal}} )</span>
        </div>
        <div class="bottom-playing-page-text">{{ playInfo ? playInfo.currentMinute : '00'}}:{{playInfo ? playInfo.currentSecond : '00'}} / {{playInfo ? playInfo.totalMinute : '00'}} : {{playInfo ? playInfo.totalSecond : '00'}}</div>
      </div>
    </div>
    <div class="bottom-btn-wrapper" @click.stop.prevent="addOrMoveShelf">
      <span class="bottom-btn-text">
      {{inShelf ? $t('detail.isAddedToShelf') : $t('detail.addOrRemoveShelf')}}
      </span>
    </div>
  </div>
</template>

<script>
import { storeShelfMixin } from '../../utils/mixin'
import { getLocalStorage } from '../../utils/localStroage'
import { addToShelf, removeFromBookShelf } from '../../utils/store'

export default {
  name: 'SpeakBottom',
  mixins: [storeShelfMixin],
  props: {
    chapter: Object,
    currentSectionIndex: Number,
    currentSectionTotal: Number,
    showPlay: Boolean,
    isPlaying: Boolean,
    playInfo: Object,
    bookItem: Object
  },
  computed: {
    // 判断该书本是否在书架内
    inShelf () {
      if (this.bookItem && this.bookShelf) {
        // 语法糖
        const flatShelf = (function flatten (arr) {
          // 将书架中的二维书架，变为一维的书架
          return [].concat(...arr.map(v => v.itemList ? [v, ...flatten(v.itemList)] : v))
        })(this.bookShelf).filter(item => item.type === 1)
        // 筛选出当前书架中与当前书本一样的书本，返回true或者是false
        const book = flatShelf.filter(item => item.fileName === this.bookItem.fileName)
        return book && book.length > 0
      } else {
        return false
      }
    }
  },
  data () {
    return {
      bookShelf: null
    }
  },
  methods: {
    addOrMoveShelf () {
      if (this.inShelf) {
        removeFromBookShelf(this.bookItem)
      } else {
        addToShelf(this.bookItem)
      }
      // 重新获取书架信息
      this.bookShelf = getLocalStorage('shelf')
    },
    // 点击是否播放
    togglePlay () {
      // 使用父组件的该方法
      this.$parent.togglePlay()
    },
    onPlayingCardClick () {
      // 向上级发送信息事件
      this.$emit('onPlayingCardClick')
    }
  },
  mounted () {
    this.bookShelf = getLocalStorage('shelf')
  }
}
</script>

<style lang="stylus" scoped>
@import '../../assets/styles/global.styl'
.bottom-wrapper
  position: absolute
  bottom: 0
  left: 0
  z-index: 110
  width: 100%
  height: px2rem(116)
  background: white
  box-shadow: 0 px2rem(-2) px2rem(2) 0 rgba(0, 0, 0 .1)
  &.hide-play
    height: px2rem(52)
  .bottom-play-wrapper
    display: flex
    width: 100%
    height: px2rem(64)
    .bottom-playing-left
      flex: 0 0 px2rem(70)
      width: px2rem(70)
      center()
      .icon-play-wrapper
        flex: 0 0 px2rem(100)
        center()
        &:active
          opacity: .5
        .icon-play_go
          font-size: px2rem(40)
          color: $color-blue
        .icon-play_pause
          font-size: px2rem(40)
          color: $color-blue
    .bottom-playing-right
      flex: 1
      padding-right: px2rem(15)
      box-sizing: border-box
      columnLeft()
      .bottom-playing-chapter-text
        max-width: px2rem(305)
        color: #333
        font-size: px2rem(16)
        line-height: px2rem(19)
        ellipsis()
      .bottom-playing-page-text
        color: #666
        font-size: px2rem(10)
        margin-top: px2rem(5)
  .bottom-btn-wrapper
    width: 100%
    height: px2rem(52)
    border-top: px2rem(1) solid #eee
    box-sizing: border-box
    center()
    .bottom-btn-text
      font-size: px2rem(14)
      color: $color-blue
      font-weight: bold
</style>
